package com.qys.livesMall.security.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Admin
 */
@Controller
@Slf4j
public class GoogleLoginController {

    /**
     * 谷歌登录相关配置
     */
    @Value("${google.login.clientId}")
    private String clientId;
    @Value("${google.login.redirectUri}")
    private String redirectUri;
    @Value("${google.login.authUrl}")
    private String authUrl;

    @GetMapping("/login/google")
    public String googleLogin() {
        Map<String, String> params = new HashMap<>();
        params.put("client_id", clientId);
        params.put("redirect_uri", redirectUri);
        params.put("response_type", "code");
        params.put("scope", "email");
        return "redirect:" + authUrl + "?" + toQueryString(params);
    }

    private String toQueryString(Map<String, ?> params) {
        StringBuilder queryString = new StringBuilder();
        for (Map.Entry<String, ?> entry : params.entrySet()) {
            if (queryString.length() > 0) {
                queryString.append("&");
            }
            queryString.append(entry.getKey()).append("=").append(entry.getValue());
        }
        return queryString.toString();
    }
}
